package domain;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import com.restfb.types.User.Education;
import com.restfb.types.User.Work;

@Entity
@Table(name = "facebookprofile")
public class FacebookProfile extends PersistentEntity {
	private long uid;
	private String name;
	private String about;
	private String bio;
	private Date birthday;
	@Column(length=4000)
	private String educations;
	private String email;
	private String gender;
	private String homeTownName;
	private String facebookProfileUrl;
	@Column(length=4000)
	private String works;
	private String profilePictureUrl;
	private Date lastUpdated;
	private boolean batchUpdated;
	
	public FacebookProfile(){
		super();
		this.lastUpdated = new Date();
		this.batchUpdated = false;
	}

	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getAbout() {
		return about;
	}

	public void setAbout(String about) {
		this.about = about;
	}

	public String getBio() {
		return bio;
	}

	public void setBio(String bio) {
		this.bio = bio;
	}

	public Date getBirthday() {
		return birthday;
	}

	public String getBirthdayAsString() {
		String birthToString = null;
		SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy", new Locale("es", "AR"));
		if (birthday != null) {
			birthToString = sf.format(birthday);
		}
		return birthToString;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getEducations() {
		return educations;
	}
	
	public Date getLastUpdated() {
		return lastUpdated;
	}

	public void setEducations(String educations) {
		this.educations = educations;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getHomeTownName() {
		return homeTownName;
	}

	public void setHomeTownName(String homeTownName) {
		this.homeTownName = homeTownName;
	}

	public String getFacebookProfileUrl() {
		return facebookProfileUrl;
	}

	public void setFacebookProfileUrl(String facebookProfileUrl) {
		this.facebookProfileUrl = facebookProfileUrl;
	}

	public String getWorks() {
		return works;
	}

	public void setWorks(String works) {
		this.works = works;
	}

	public String getProfilePictureUrl() {
		return profilePictureUrl;
	}

	public void setProfilePictureUrl(String profilePictureUrl) {
		this.profilePictureUrl = profilePictureUrl;
	}
	
	public void setLastUpdated(Date lastUpdated) {
		this.lastUpdated = lastUpdated;
	}

	public boolean isBatchUpdated() {
		return batchUpdated;
	}

	public void setBatchUpdated(boolean batchUpdated) {
		this.batchUpdated = batchUpdated;
	}

	@Override
	public String toString() {
		return "Profile [id=" + this.uid + "]";
	}
	
	public void setImportedProfile(FacebookUser u) {
		this.uid = Long.parseLong(u.getId());
		this.about = u.getAbout();
		this.bio = u.getBio();
		this.birthday = u.getBirthdayAsDate();
		this.educations = getConvertedEducations(u.getEducation());
		this.email = u.getEmail();
		this.facebookProfileUrl = u.getLink();
		this.gender = u.getGender();
		this.homeTownName = u.getHometownName();
		this.name = u.getName();
		this.works = getConvertedWorks(u.getWork());
		this.profilePictureUrl = u.getPic();
		this.lastUpdated = u.getUpdatedTime();
	}
	
	public static FacebookProfile buildFacebookProfile(FacebookUser profile) {
		FacebookProfile fbProfile = new FacebookProfile();
		fbProfile.setUid(Long.parseLong(profile.getId()));
		fbProfile.setAbout(profile.getAbout());
		fbProfile.setBio(profile.getBio());
		fbProfile.setBirthday(profile.getBirthdayAsDate());
		fbProfile.setEducations(getConvertedEducations(profile.getEducation()));
		fbProfile.setEmail(profile.getEmail());
		fbProfile.setFacebookProfileUrl(profile.getLink());
		fbProfile.setGender(profile.getGender());
		fbProfile.setHomeTownName(profile.getHometownName());
		fbProfile.setName(profile.getName());
		fbProfile.setWorks(getConvertedWorks(profile.getWork()));
		fbProfile.setProfilePictureUrl(profile.getPic());
		fbProfile.setLastUpdated(profile.getUpdatedTime());
		return fbProfile;
	}
	
	private static String getConvertedEducations(List<Education> educations) {
		String educationsString = null;
		if (!(educations == null || educations.isEmpty())) {
			educationsString = "<ul>";
			for (Education e: educations) {
				educationsString += "<li>";
				educationsString += e.getSchool().getName();
				if (e.getDegree() != null) {
					educationsString += ", " + e.getDegree();
				}
				if (e.getYear() != null) {
					educationsString += ", Graduated: " + e.getYear().getName();
				}
				educationsString += "</li>";
			}
			educationsString += "</ul>";
		}
		return educationsString;
	}
	
	private static String getConvertedWorks(List<Work> works) {
		String worksString = null;
		if (!(works == null || works.isEmpty())) {
			worksString = "<ul>";
			for (Work w: works) {
				worksString += "<li>";
				if (w.getPosition() != null) {
					worksString += w.getPosition().getName() + " en: " + w.getEmployer().getName();
				} else {
					Date today = new Date();
					if (w.getEndDate() != null && w.getEndDate().compareTo(today) < 0) { 
						worksString += "trabajo en: " + w.getEmployer().getName();
						if (w.getStartDate() != null) {
							worksString += ", desde: " + w.getStartDate().toString();
						}
						worksString += ", hasta: " + w.getEndDate().toString();
					} else {
						worksString += "trabaja en: " + w.getEmployer().getName();
					}
				}
				worksString += "</li>";
			}
			worksString += "</ul>";
		}
		return worksString;
	}
	
}